package com.qf.fmmall.utils;

import io.jsonwebtoken.*;

import java.util.Date;

public class JwtUtils {

//    public static final String subject="jack";
//    public static final String id="100";
    public  static final String secretkey="QIANfeng6666";

    //生成token
    public static String createtoken(String subject,String tokenid){
        JwtBuilder builder = Jwts.builder();

        String token = builder.setSubject(subject)//主题-->就是token中携带的数据
                .setIssuedAt(new Date())//设置token的生成时间
                .setId(tokenid)//设置用户id为token  id
                .setExpiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000)) //设置过期时间
                .signWith(SignatureAlgorithm.HS256, secretkey)//设置加密方式和加密密码
                .compact();
        return token;
    }

    //解析token
    public static boolean parsetoken(String token){
        boolean result=true;
        JwtParser parser = Jwts.parser();
        try {
            Jwt jwt = parser.setSigningKey(secretkey)
                    .parse(token);
        } catch (Exception e) {
            e.printStackTrace();
            result=false;
        }
        return result;

    }


    public static void main(String[] args) {
        String token = createtoken("jack", "100");
        System.out.println(token);
        boolean b = parsetoken(token);
        System.out.println(b);
    }
}
